package com.galen.manual.controller;

import com.galen.manual.common.Result;
import com.galen.manual.dto.ProductCreateDTO;
import com.galen.manual.dto.ProductQueryDTO;
import com.galen.manual.vo.ProductDetailVO;
import com.galen.manual.vo.ProductListVO;
import com.galen.manual.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@Tag(name = "手工品管理", description = "手工品相关接口")
@RestController
@RequestMapping("/products")
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @Operation(summary = "获取手工品列表", description = "根据条件查询手工品列表")
    @GetMapping
    public Result<List<ProductListVO>> getProductList(
            @Parameter(description = "查询条件") ProductQueryDTO queryDTO) {
        List<ProductListVO> products = productService.getProductList(queryDTO);
        return Result.success(products);
    }

    @Operation(summary = "获取手工品详情", description = "根据ID获取手工品详细信息")
    @GetMapping("/{id}")
    public Result<ProductDetailVO> getProductDetail(
            @Parameter(description = "手工品ID", required = true) @PathVariable Long id) {
        ProductDetailVO product = productService.getProductDetail(id);
        return Result.success(product);
    }

    @Operation(summary = "创建手工品", description = "创建新的手工品")
    @PostMapping
    public Result<Void> createProduct(
            @Parameter(description = "手工品信息", required = true) @Valid @RequestBody ProductCreateDTO createDTO) {
        productService.createProduct(createDTO);
        return Result.success("创建成功");
    }

    @Operation(summary = "更新手工品", description = "更新手工品信息")
    @PutMapping("/{id}")
    public Result<Void> updateProduct(
            @Parameter(description = "手工品ID", required = true) @PathVariable Long id,
            @Parameter(description = "手工品信息", required = true) @Valid @RequestBody ProductCreateDTO updateDTO) {
        productService.updateProduct(id, updateDTO);
        return Result.success("更新成功");
    }

    @Operation(summary = "上架手工品", description = "将手工品状态改为上架")
    @PutMapping("/{id}/online")
    public Result<Void> onlineProduct(
            @Parameter(description = "手工品ID", required = true) @PathVariable Long id) {
        productService.onlineProduct(id);
        return Result.success("上架成功");
    }

    @Operation(summary = "下架手工品", description = "将手工品状态改为下架")
    @PutMapping("/{id}/offline")
    public Result<Void> offlineProduct(
            @Parameter(description = "手工品ID", required = true) @PathVariable Long id) {
        productService.offlineProduct(id);
        return Result.success("下架成功");
    }

    @Operation(summary = "上传手工品图片", description = "上传手工品图片文件")
    @PostMapping("/upload")
    public Result<String> uploadImage(
            @Parameter(description = "图片文件", required = true) @RequestParam("file") MultipartFile file,
            HttpServletRequest request) {
        try {
            String imageUrl = productService.uploadImage(file, request);
            return Result.success("图片上传成功", imageUrl);
        } catch (Exception e) {
            return Result.error("图片上传失败: " + e.getMessage());
        }
    }

    @Operation(summary = "批量上传手工品图片", description = "批量上传手工品图片文件")
    @PostMapping("/upload/batch")
    public Result<List<String>> uploadImages(
            @Parameter(description = "图片文件列表", required = true) @RequestParam("files") MultipartFile[] files,
            HttpServletRequest request) {
        try {
            List<String> imageUrls = productService.uploadImages(files, request);
            return Result.success("图片批量上传成功", imageUrls);
        } catch (Exception e) {
            return Result.error("图片批量上传失败: " + e.getMessage());
        }
    }

}